﻿
Imports osi.root.connector
Imports osi.root.utils
Imports osi.root.procedure
Imports osi.root.formation

Partial Public Class device_pool(Of T)
    Protected Overridable Function is_valid_device(ByVal c As T) As Boolean
        Return validator Is Nothing OrElse
               validator(c)
    End Function

    Protected Overridable Sub close_device(ByVal c As T)
        If Not closer Is Nothing Then
            closer(c)
            assert(Not is_valid_device(c))
        End If
    End Sub

    Protected Overridable Function create_device(ByVal r As pointer(Of T)) As event_comb
        Dim ec As event_comb = Nothing
        Return New event_comb(Function() As Boolean
                                  assert(Not ctor Is Nothing)
                                  ec = ctor(r)
                                  Return waitfor(ec) AndAlso
                                         goto_next()
                              End Function,
                              Function() As Boolean
                                  Return ec.end_result() AndAlso
                                         goto_end()
                              End Function)
    End Function

    Protected Overridable Function device_identity(ByVal c As T) As String
        If device_id Is Nothing Then
            Return Convert.ToString(c)
        Else
            Return device_id(c)
        End If
    End Function
End Class
